package com.feinno.urcs.group.services;

import com.feinno.urcs.group.dao.GlobalGroupListDao;
import com.feinno.urcs.group.entity.*;
import com.feinno.urcs.group.util.URIUtils;
import com.feinno.urcs.sip.UrcsSipFactory;
import com.feinno.urcs.types.MobileNo;
import com.feinno.util.Tuple3;
import com.google.gson.Gson;
import org.helium.framework.annotations.FieldSetter;
import org.helium.framework.annotations.ServiceImplementation;
import org.helium.framework.annotations.ServiceSetter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

/**
 * @author Li.Hongbo <lihongbo@feinno.com>
 */

@ServiceImplementation
@WebService
@Deprecated
public class GroupListServiceImplBak implements GroupListServiceBak {

	private static final Logger LOGGER = LoggerFactory.getLogger(GroupListServiceImplBak.class);

	@ServiceSetter
	private GlobalGroupListDao groupListDao;

	@FieldSetter("${WEB_SERVICES_ADDRESS}")
	private String webServicesAddress;

	@Override
	public GetGroupListResp getGroupList(@WebParam(name = "MySoapHeader",header=true) MySoapHeader sunLeDog,@WebParam(name = "GetGroupListReq") GetGroupListReq args){
		LOGGER.info(String.format("getGroupList start  args =%s ", new Gson().toJson(args)));
		GetGroupListResp resp = new GetGroupListResp();
		ResultInfo info = new ResultInfo();
		try {
			resp.setSeqNo(args.getSeqNo());
			info.setResultCode(200);
			info.setDescription("OK");
			String mobileNo = URIUtils.getMobileNo(UrcsSipFactory.getAddressFactory().createURI(args.getUserId()));
			MobileNo userMobile = MobileNo.parseFrom(mobileNo);
//			resp.setGroupListInfos(groupListDao.getGroupList(userMobile.getMobile()));
			resp.setResult(info);
		} catch (Exception e) {
			LOGGER.error("getGroupList error : {}", e);
			info.setResultCode(500);
			info.setDescription("SERVER ERROR");
			resp.setSeqNo(args.getSeqNo());
			resp.setResult(info);
		}
		LOGGER.info(String.format("getGroupList end args =%s ,result=%s", new Gson().toJson(args), new Gson().toJson(resp)));
		return resp;
	}

	@Override
	public GroupListResp addGroup(AddGroupReq args) {
		LOGGER.info(String.format("addGroup start  args =%s ", new Gson().toJson(args)));
		GroupListResp resp = new GroupListResp();
		ResultInfo info = new ResultInfo();
		try {
			info.setResultCode(200);
			info.setDescription("OK");
			resp.setResult(info);
			resp.setSeqNo(args.getSeqNo());
			String mobileNo = URIUtils.getMobileNo(UrcsSipFactory.getAddressFactory().createURI(args.getUserId()));
			MobileNo userMobile = MobileNo.parseFrom(mobileNo);
			groupListDao.addGroup(userMobile.getMobile(), args.getGroupListInfo());
		} catch (Exception ex) {
			LOGGER.error("addGroup error : {}", ex);
			info.setResultCode(500);
			info.setDescription("SERVER ERROR");
			resp.setResult(info);
			resp.setSeqNo(args.getSeqNo());
		}
		LOGGER.info(String.format("addGroup end args =%s ,result=%s", new Gson().toJson(args), new Gson().toJson(resp)));
		return resp;
	}

	@Override
	public GroupListResp delGroup(DelGroupReq args) {
		LOGGER.info(String.format("delGroup start  args =%s ", new Gson().toJson(args)));
		GroupListResp resp = new GroupListResp();
		ResultInfo info = new ResultInfo();
		try {
			String mobileNo = URIUtils.getMobileNo(UrcsSipFactory.getAddressFactory().createURI(args.getUserId()));
			MobileNo userMobile = MobileNo.parseFrom(mobileNo);
			groupListDao.delGroup(userMobile.getMobile(), args.getGroupUri());
			info.setResultCode(200);
			info.setDescription("OK");
			resp.setResult(info);
			resp.setSeqNo(args.getSeqNo());
		} catch (Exception ex) {
			LOGGER.error("delGroup error : {}", ex);
			info.setResultCode(500);
			info.setDescription("SERVER ERROR");
			resp.setResult(info);
			resp.setSeqNo(args.getSeqNo());
		}
		LOGGER.info(String.format("delGroup end args =%s ,result=%s", new Gson().toJson(args), new Gson().toJson(resp)));
		return resp;
	}

	@Override
	public GroupListResp updGrpName(UpdGrpNameReq args) {
		LOGGER.info(String.format("updGrpName start  args =%s ", new Gson().toJson(args)));
		GroupListResp resp = new GroupListResp();
		ResultInfo info = new ResultInfo();
		try {
			String mobileNo = URIUtils.getMobileNo(UrcsSipFactory.getAddressFactory().createURI(args.getUserId()));
			MobileNo userMobile = MobileNo.parseFrom(mobileNo);
			groupListDao.updateGroupName(userMobile.getMobile(), args.getGroupUri(), args.getGroupName());
			info.setResultCode(200);
			info.setDescription("OK");
			resp.setResult(info);
			resp.setSeqNo(args.getSeqNo());
		} catch (Exception ex) {
			LOGGER.error("updGrpName error : {}", ex);
			info.setResultCode(500);
			info.setDescription("SERVER ERROR");
			resp.setResult(info);
			resp.setSeqNo(args.getSeqNo());
		}
		LOGGER.info(String.format("updGrpName end args =%s ,result=%s", new Gson().toJson(args), new Gson().toJson(resp)));
		return resp;
	}


	@Override
	public GroupListResp chgGrpAdmin(ChgGrpAdminReq args) {
		LOGGER.info(String.format("chgGrpAdmin start  args =%s ", new Gson().toJson(args)));
		GroupListResp resp = new GroupListResp();
		ResultInfo info = new ResultInfo();
		try {
			String mobileNo = URIUtils.getMobileNo(UrcsSipFactory.getAddressFactory().createURI(args.getUserId()));
			MobileNo userMobile = MobileNo.parseFrom(mobileNo);
			groupListDao.changeGroupAdmin(userMobile.getMobile(), args.getGroupUri(), args.isAdmin());
			info.setResultCode(200);
			info.setDescription("OK");
			resp.setResult(info);
			resp.setSeqNo(args.getSeqNo());
		} catch (Exception ex) {
			LOGGER.error("chgGrpAdmin error : {}", ex);
			info.setResultCode(500);
			info.setDescription("SERVER ERROR");
			resp.setResult(info);
			resp.setSeqNo(args.getSeqNo());
		}
		LOGGER.info(String.format("chgGrpAdmin end args =%s ,result=%s", new Gson().toJson(args), new Gson().toJson(resp)));
		return resp;
	}

	@Override
	public QryGrpCountResp qryGrpCount(QryGrpCountReq args) {
		LOGGER.info(String.format("qryGrpCount start  args =%s ", new Gson().toJson(args)));
		QryGrpCountResp resp = new QryGrpCountResp();
		UserGroupCount userGroupCount = new UserGroupCount();
		try {
			userGroupCount.setResultCode(200);
			userGroupCount.setDescription("OK");
			String mobileNo = URIUtils.getMobileNo(UrcsSipFactory.getAddressFactory().createURI(args.getUserId()));
			MobileNo userMobile = MobileNo.parseFrom(mobileNo);
			Tuple3<Integer, Integer, Integer> combo3 = groupListDao.queryGroupCount(userMobile.getMobile());
			userGroupCount.setCreateGroupCount(combo3.getV1());
			userGroupCount.setJoinGroupCount(combo3.getV2());
			userGroupCount.setManageGroupCount(combo3.getV3());
		} catch (Exception ex) {
			LOGGER.error("qryGrpCount error : {}", ex);
			userGroupCount.setResultCode(500);
			userGroupCount.setDescription("SERVER ERROR");
		}
		resp.setSeqNo(args.getSeqNo());
		resp.setUserGroupCount(userGroupCount);
		LOGGER.info(String.format("addGroup end args =%s ,result=%s", new Gson().toJson(args), new Gson().toJson(resp)));
		return resp;
	}


	@Override
	public GroupListResp dismissGroup(DismissGroupReq args) {
		LOGGER.info(String.format("addGroup start  args =%s ", new Gson().toJson(args)));
		GroupListResp resp = new GroupListResp();
		ResultInfo info = new ResultInfo();
		try {
			groupListDao.dismissGroup(args.getGroupUri());
			info.setResultCode(200);
			info.setDescription("OK");
			resp.setResult(info);
			resp.setSeqNo(args.getSeqNo());
		} catch (Exception ex) {
			LOGGER.error("dismissGroup error : {}", ex);
			info.setResultCode(500);
			info.setDescription("SERVER ERROR");
			resp.setResult(info);
			resp.setSeqNo(args.getSeqNo());
		}
		LOGGER.info(String.format("addGroup end args =%s ,result=%s", new Gson().toJson(args), new Gson().toJson(resp)));
		return resp;
	}

	public void start() {
		LOGGER.info(String.format("WebStart start ....  URL=%s", webServicesAddress));
		Endpoint.publish(webServicesAddress, this);
		LOGGER.info(String.format("WebStart start success  URL=%s", webServicesAddress));

	}

	public static void main(String args[]) {
		GroupListServiceImplBak impl = new GroupListServiceImplBak();
		Endpoint.publish("0.0.0.0:9000/GroupListService", impl);
	}
}
